package com.example.taobaou.presenter.impl;

import com.example.taobaou.model.Api;
import com.example.taobaou.model.domain.SelectedContent;
import com.example.taobaou.model.domain.SelectedPage;
import com.example.taobaou.presenter.ISelectedPagePresenter;
import com.example.taobaou.utils.LogUtils;
import com.example.taobaou.utils.RetrofitManager;
import com.example.taobaou.utils.UrlUtils;
import com.example.taobaou.view.ISelectedPageCallback;

import org.jetbrains.annotations.NotNull;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SelectedPagePresenterImpl implements ISelectedPagePresenter {
    private ISelectedPageCallback mViewCallback = null;
    private final Api mApi;

    public SelectedPagePresenterImpl() {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getCategories() {

        if (mViewCallback != null) {
            mViewCallback.onLoading();
        }

        Call<SelectedPage> task = mApi.getSelectedPageCategories();

        task.enqueue(new Callback<SelectedPage>() {
            @Override
            public void onResponse(Call<SelectedPage> call, Response<SelectedPage> response) {
                int code = response.code();
                LogUtils.d(SelectedPagePresenterImpl.this, "请求成功，code--------->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    //请求成功
                    SelectedPage result = response.body();
                    //通知ui更新
                    if (mViewCallback != null) {
                        mViewCallback.onCategoriesLoaded(result);
                    }
                } else {
                    //请求失败
                    LogUtils.d(SelectedPagePresenterImpl.this, "请求失败---------------------->");
                    onLoadError();
                }
            }

            @Override
            public void onFailure(Call<SelectedPage> call, Throwable t) {
                //请求失败
                LogUtils.d(SelectedPagePresenterImpl.this, "请求失败---------------------->" + t);
                onLoadError();
            }
        });
    }

    @Override
    public void getContentByCategory(@NotNull SelectedPage.DataBean item) {

        int catectedId = item.getFavorites_id();
        LogUtils.d(this, "categoryId -= > " + catectedId);
        String tergeUrl = UrlUtils.getSelctedPageContentUrl(catectedId);
        Call<SelectedContent> task = mApi.getSelectedPageContent(tergeUrl);
        LogUtils.d(this, "targetUrl--->" + task);
        task.enqueue(new Callback<SelectedContent>() {
            @Override
            public void onResponse(Call<SelectedContent> call, Response<SelectedContent> response) {
                int itemCode = response.code();
                LogUtils.d(SelectedPagePresenterImpl.this, "itemcode--------->" + itemCode);
                if (itemCode == HttpURLConnection.HTTP_OK) {
                    SelectedContent result = response.body();
                    if (mViewCallback != null) {
                        mViewCallback.onContentLoad(result);
                        LogUtils.d(SelectedPagePresenterImpl.this, "chengong --------------------------------------------------------------------->" );

                    }
                } else {
                    LogUtils.d(SelectedPagePresenterImpl.this, "请求失败11---------------------->");
                    onLoadError();
                }
            }

            @Override
            public void onFailure(Call<SelectedContent> call, Throwable t) {
                LogUtils.d(SelectedPagePresenterImpl.this, "请求失败22---------------------->" + t);
                onLoadError();
            }
        });
    }

    @Override
    public void reloadContent() {
       this.getCategories();
    }

    @Override
    public void registerViewCallback(ISelectedPageCallback callback) {
        this.mViewCallback = callback;
    }

    @Override
    public void unregisiterViewCallback(ISelectedPageCallback callback) {
        this.mViewCallback = null;
    }

    private void onLoadError() {
        if (mViewCallback != null) {
            mViewCallback.onNatworkError();
        }
    }
}
